﻿package bdz.projects.lavender.doi.utils{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.Video;
	import flash.display.BlendMode;
	import gs.easing.*;
	import gs.*;
	
	import bdz.common.events.GenericEvent;
	/**
	 * ...
	 * @author Donald
	 */
	public class DOIVideo extends Sprite {
		private var holder:Bitmap;
		private var vsource:Video;
		private var drawing:BitmapData;
		function DOIVideo() {
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		private function initializeComponent(evt:Event):void {
			
			mouseEnabled = false;
			cacheAsBitmap = true;
			holder = new Bitmap(new BitmapData(1024, 576, true, 0x00ffffff), "never", false);
			holder.cacheAsBitmap = true;
			addChild(holder);
		}
		private function render(evt:GenericEvent):void {
			try {
				drawing.dispose();
			}catch (e:Error){}
			drawing = new BitmapData(vsource.width, vsource.height, true , 0x00ffffff);
			drawing.draw(vsource);
			
			try {
				holder.bitmapData.dispose();
			}catch (e:Error){}
			holder.bitmapData = drawing;
			
		}
		public function renderVideo(v:Video):void {
			parent.setChildIndex(this, parent.numChildren-1);
			vsource = v;
			holder.alpha = 1;
			
			DOIGlobalRender.getInstance().addEventListener(GenericEvent.PROGRESS, render);
		}
		public function stopRendering():void {
			DOIGlobalRender.getInstance().removeEventListener(GenericEvent.PROGRESS, render);
			holder.bitmapData.dispose();
			vsource = null;
		}
		public function cleanUp():void {
			try {
				drawing.dispose();
			}catch (e:Error){}
			try {
				holder.bitmapData.dispose();
			}catch (e:Error) { }
			
		}
		public function fadeOut():void {
			TweenMax.to(holder, .6, { alpha:0, ease:Strong.easeInOut, onComplete:cleanUp } );
		}
		public function set position(o:Object):void {
			holder.x = o.x;
			holder.y = o.y;
		}
		
	}
	
}